package com.zlk.algorithm.dataStructure.graph.impl;

/**
 * @program: algorithm
 * @ClassName GraphMatrix
 * @description:邻接矩阵实现
 * @author: slfang
 * @create: 2024-03-20 11:12
 * @Version 1.0
 **/
public class GraphMatrix {

    /**
     * GRAPH_MAX表示无法到达
     */
    private static final int GRAPH_MAX=Integer.MAX_VALUE;
    int size;
    int[][] matrix;

    public GraphMatrix(int size){
        this.size = size;
        matrix = new int[size][size];
    }

    public static void main(String[] args) {
        GraphMatrix graph = new GraphMatrix(9);
        /*创建数组*/
        graph.createDate(graph);
    }

    /**
     * 创建图
     * @param graph
     */
    private void createDate(GraphMatrix graph) {
        int[] a0 = new int[]{0,GRAPH_MAX,GRAPH_MAX,GRAPH_MAX,6};
        int[] a1 = new int[]{9,0,3,GRAPH_MAX,GRAPH_MAX};
        int[] a2 = new int[]{2,GRAPH_MAX,0,5,GRAPH_MAX};
        int[] a3 = new int[]{GRAPH_MAX,GRAPH_MAX,GRAPH_MAX,0,1};
        int[] a4 = new int[]{GRAPH_MAX,GRAPH_MAX,GRAPH_MAX,GRAPH_MAX,0};
        graph.matrix[0]=a0;
        graph.matrix[1]=a1;
        graph.matrix[2]=a2;
        graph.matrix[3]=a3;
        graph.matrix[4]=a4;
    }

    /**
     * 横向看出度，纵向看入度
     * 获取一个顶点的度
     * @return
     */
    public int getDegreeByVertex(int vertexIndex, int type, GraphMatrix graph) {
        int count=0;
        if(0==type){//出度
            for (int i = 0; i <graph.matrix[vertexIndex].length ; i++) {
                if(graph.matrix[vertexIndex][i]!=0&&graph.matrix[vertexIndex][i]!=GRAPH_MAX){
                    count++;
                }
            }
        }else if(1==type){//入度
            for (int i = 0; i < graph.size; i++) {
                if(graph.matrix[i][vertexIndex]!=0&&graph.matrix[i][vertexIndex]!=GRAPH_MAX){
                    count++;
                }
            }
        }else{
            return -1;
        }
        return count;
    }

}
